package com.example.controller;


import com.example.dao.ShenqingDao;

import com.example.pojo.Shenqing;
import com.example.pojo.Userinfo;
import com.example.util.ResultUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.tagext.PageData;
import java.util.Iterator;
import java.util.List;


/**
 * (Shenqing)表控制层
 *
 * @author makejava

 */
@Controller
public class ShenqingController {
    @Resource
    ShenqingDao shenqingDao;




    /**
     * 跳转到列表页面
     *
     * @return
     */
    @RequestMapping("manage/shenqingList")
    public String shenqingList() {
        return "shenqingList";
    }

    /**
     * 跳转到列表页面
     *
     * @return
     */
    @RequestMapping("manage/pyList")
    public String pyList() {
        return "pingyiList";
    }

    /**
     * 跳转到添加页面
     *
     * @return
     */
    @RequestMapping("manage/addShenqing")
    public String addShenqing(Model model) {
        return "saveShenqing";
    }



    /**
     * 跳转到修改页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("manage/editShenqing")
    public String editShenqing(Integer id, Model model) {
        Shenqing shenqing = shenqingDao.queryById(id);
        model.addAttribute("shenqing", shenqing);
        return "saveShenqing";
    }

    /**
     * 跳转到投票页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("manage/tp")
    public String tp(Integer id, Model model) {

        Shenqing shenqing = shenqingDao.queryById(id);
        model.addAttribute("shenqing", shenqing);
        return "saveToupiaoinfo";
    }
    /**
     * 跳转到修改页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("manage/feditShenqing")
    public String feditShenqing(Integer id, Model model) {
        Shenqing shenqing = shenqingDao.queryById(id);
        model.addAttribute("shenqing", shenqing);
        return "fsh";
    }


    /**
     * 跳转到修改页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("manage/geditShenqing")
    public String geditShenqing(Integer id, Model model) {
        Shenqing shenqing = shenqingDao.queryById(id);
        model.addAttribute("shenqing", shenqing);
        return "gsh";
    }

    /**
     * 查看详情页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("manage/shenqingInfo")
    public String shenqingInfo(Integer id, Model model) {
        Shenqing shenqing = shenqingDao.queryById(id);
        model.addAttribute("shenqing", shenqing);
        return "shenqingInfo";
    }


    /**
     * 分页查询
     *
     * @param page  默认第一页
     * @param limit 默认每页显示10条
     * @return
     */
    @RequestMapping("manage/queryShenqingList")
    @ResponseBody
    public ResultUtil getCarouseList(Integer page, Integer limit, String zhuanye,String name,HttpSession session) {
        if (null == page) { //默认第一页
            page = 1;
        }
        if (null == limit) { //默认每页10条
            limit = 10;
        }
        Shenqing shenqing = new Shenqing();
        Userinfo loginAdmin = (Userinfo)session.getAttribute("loginAdmin");
        String role = loginAdmin.getRole();
        shenqing.setZhuanye(zhuanye);
        shenqing.setName(name);
        if ("学生".equals(role)){//学生只查看自己的申请
            shenqing.setName(loginAdmin.getName());
        }
        if ("辅导员".equals(role)){//辅导员只查看自己专业的申请
            shenqing.setZhuanye(loginAdmin.getMoney());
        }

        PageHelper.startPage(page, limit, true);
        List<Shenqing> list = shenqingDao.queryAllByLimit(shenqing);
        PageInfo<Shenqing> pageInfo = new PageInfo<Shenqing>(list);  //使用mybatis分页插件
        ResultUtil resultUtil = new ResultUtil();
        resultUtil.setCode(0);  //设置返回状态0为成功
        resultUtil.setCount(pageInfo.getTotal());  //获取总记录数目 类似count(*)
        resultUtil.setData(pageInfo.getList());    //获取当前查询出来的集合
        return resultUtil;
    }

    /**
     * 分页查询
     *
     * @param page  默认第一页
     * @param limit 默认每页显示10条
     * @return
     */
    @RequestMapping("manage/queryShenqingList1")
    @ResponseBody
    public ResultUtil getCarouseList1(Integer page, Integer limit, String zhuanye,String name,HttpSession session) {
        if (null == page) { //默认第一页
            page = 1;
        }
        if (null == limit) { //默认每页10条
            limit = 10;
        }
        Shenqing shenqing = new Shenqing();
        Userinfo loginAdmin = (Userinfo)session.getAttribute("loginAdmin");

        shenqing.setZhuanye(zhuanye);
        shenqing.setName(name);
        shenqing.setStatus("审核完成");

        PageHelper.startPage(page, limit, true);
        List<Shenqing> list = shenqingDao.queryAllByLimit(shenqing);
        Iterator<Shenqing> it = list.iterator();
        while(it.hasNext()){//移除自己的申请
            Shenqing next = it.next();
            if(next.getName().equals(loginAdmin.getName())){
                it.remove();
            }
        }
        PageInfo<Shenqing> pageInfo = new PageInfo<Shenqing>(list);  //使用mybatis分页插件
        ResultUtil resultUtil = new ResultUtil();
        resultUtil.setCode(0);  //设置返回状态0为成功
        resultUtil.setCount(pageInfo.getTotal());  //获取总记录数目 类似count(*)
        resultUtil.setData(pageInfo.getList());    //获取当前查询出来的集合
        return resultUtil;
    }


    /**
     * 插入记录
     */
    @RequestMapping("manage/saveShenqing")
    @ResponseBody
    public ResultUtil saveShenqing(Shenqing shenqing, HttpSession session) {
        
        try {
            shenqingDao.insert(shenqing);
            return ResultUtil.ok("添加申请信息成功");
        } catch (Exception e) {
            return ResultUtil.error("添加申请信息出错,稍后再试！");
        }
    }

    /**
     * 更新记录
     */
    @RequestMapping("manage/updateShenqing")
    @ResponseBody
    public ResultUtil updateShenqing(Shenqing shenqing, HttpSession session) {

        try {
            shenqingDao.update(shenqing);
            return ResultUtil.ok("修改申请信息成功");
        } catch (Exception e) {
            return ResultUtil.error("修改申请信息出错,稍后再试！");
        }
    }


    /**
     * 根据ID删除
     *
     * @param id
     * @return
     */
    @RequestMapping("manage/deleteShenqing")
    @ResponseBody
    public ResultUtil deleteShenqingById(Integer id) {
        try {
            shenqingDao.deleteById(id);
            return ResultUtil.ok("删除申请信息成功");
        } catch (Exception e) {
            return ResultUtil.error("删除申请信息出错,稍后再试！");
        }
    }

    /**
     * 根据ID批量删除
     *
     * @param idsStr
     * @return
     */
    @RequestMapping("manage/deletesShenqing")
    @ResponseBody
    public ResultUtil deletesShenqing(String idsStr) {
        try {
            if (!StringUtils.isBlank(idsStr)) {
                String[] ids = idsStr.split(",");
                for (String id : ids) {
                    shenqingDao.deleteById(Integer.parseInt(id));
                }
            }
            return ResultUtil.ok("批量删除申请信息成功");
        } catch (Exception e) {
            return ResultUtil.error("删除申请信息出错,稍后再试！");
        }
    }



}

